package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CompanyImpl implements CompanyDao {
    @Override
    public Integer add(CompanyModel companyModel) {
        String sql = "insert into company....";

        return JDBC.update(sql);
    }

    @Override
    public Integer update(CompanyModel companyModel) {
        String sql = "UPDATE `user` SET " +
                "`username` = '', " +
                "`password` = '1', " +
                "`phone` = '1', " +
                "`sex` = '1', " +
                "`birthday` = '1', " +
                "`hobby` = '1', " +
                "`introduce` = '1', " +
                "`remark` = '1', " +
                "`update_time` = '1', " +
                "`create_time` = '1', " +
                "`enable` = '1', " +
                "`email` = '1', " +
                "`height` = '1', " +
                "`address` = '1' " +
                "WHERE (`id` = '86');\n";

        return JDBC.update(sql);
    }

    @Override
    public Integer delete(CompanyModel companyModel) {
        String sql = "delete from company where id=" + companyModel.getId();

        return JDBC.update(sql);
    }

    @Override
    public CompanyModel findById(CompanyModel companyModel) {
        String sql = "select * from company where id=" + companyModel.getId();
        CompanyModel model = null;
//        ResultSet resultSet = JDBC.query(sql);
//        try {
//            while (resultSet.next()) {
//                model = new CompanyModel();
//                model.setId(resultSet.getInt("id"));
//                model.setLogo(resultSet.getString("logo"));
//                model.setPhone(resultSet.getString("phone"));
//                model.setNet(resultSet.getString("net"));
//                model.setEmail(resultSet.getString("email"));
//                model.setLinePhone(resultSet.getString("line_phone"));
//                model.setAddress(resultSet.getString("address"));
//                model.setVersion(resultSet.getString("version"));
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return model;
        return setValue(sql).get(0);
    }

    @Override
    public List<CompanyModel> findAll(CompanyModel companyModel) {
        String sql = "select * from company ";
        return setValue(sql);
    }

    @Override
    public CompanyModel getCompanyModel() {
        String sql = "select * from company where is_show=1";
        List<CompanyModel> list = setValue(sql);
        return list.size() > 0 ? list.get(0) : null;
    }

    private List<CompanyModel> setValue(String sql) {
        ResultSet resultSet = JDBC.query(sql);
        List<CompanyModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                CompanyModel model = new CompanyModel();
                model.setId(resultSet.getInt("id"));
                model.setLogo(resultSet.getString("logo"));
                model.setPhone(resultSet.getString("phone"));
                model.setNet(resultSet.getString("net"));
                model.setEmail(resultSet.getString("email"));
                model.setLinePhone(resultSet.getString("line_phone"));
                model.setAddress(resultSet.getString("address"));
                model.setVersion(resultSet.getString("version"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        if (list.size() == 0) {
//            list.add(null);
//        }
        return list;
    }
}
